package com.property.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.mapper.UserMapper;
import com.property.pojo.User;
import com.property.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByName(String username) {
        return userMapper.findUserByName(username);
    }

    @Override
    public String  findAllUser(Integer page) {
        PageHelper.startPage(page,10);
        List<User> list = userMapper.findAllUser();
        PageInfo pageInfo = new PageInfo(list);
        return JSON.toJSONString(pageInfo);
    }
    //停用
    @Override
    public int stopUser(Integer uId) {
        return userMapper.stopUser(uId);
    }
    //启用
    @Override
    public int startUser(Integer uId) {
        return userMapper.startUser(uId);
    }

    @Override
    public int deleteUserByuId(Integer uId) {
        return userMapper.deleteUserByuId(uId);
    }

    @Override
    public User findByuId(Integer uId) {
        return userMapper.findByuId(uId);
    }

    //新增更新
    @Override
    public String updateOrSaveUser(User user,String password2) {
        String messgae="操作成功";
        int num =0;
        //判断数据是否为空
        if(user == null || password2 ==null || password2.equals("")){
            messgae="输入信息不能为空";
        }else if(!user.getuPassword().equals(password2)){
            messgae="两次密码不一致";
        }else {
            if(user.getuId() !=null && user.getuId() !=0){  //更新
                num = userMapper.updateUser(user);
            }else{
                if(userMapper.findUserByName(user.getuName())!=null){ //判断用户是否存在
                    messgae="用户名已存在";
                }else {
                    num = userMapper.addUser(user);
                }
            }
            if(num <=0){
                messgae = "操作失败";
            }
        }
        return JSON.toJSONString(messgae);
    }

}
